package com.aplink.generic.adapter;

import java.util.ArrayList;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;

import com.aplink.generic.PhotoFragment;
import com.aplink.generic.bitmap.ImageFetcher;
import com.aplink.generic.network.GenericFile;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.pojo.controlspec.GallerySpec;

public class GalleryPagerAdapter extends FragmentStatePagerAdapter {
    private final GallerySpec mGallerySpec;
    private ArrayList<GenericObject> mGenericObjects;
    private final ImageFetcher mImageFetcher;

    public GalleryPagerAdapter(final FragmentManager fm,
            final ArrayList<GenericObject> genericObjects,
            final GallerySpec gallerySpec, final ImageFetcher imageFetcher) {
        super(fm);
        this.mGenericObjects = genericObjects;
        this.mGallerySpec = gallerySpec;
        this.mImageFetcher = imageFetcher;
    }

    public GenericObject add(final String imageURL) {
        final GenericObject genericObject = new GenericObject();
        genericObject.put(this.mGallerySpec.getImagePathColumn(), imageURL);
        this.mGenericObjects.add(genericObject);
        notifyDataSetChanged();
        return genericObject;
    }

    public GenericObject delete(final int position) {
        if (position >= mGenericObjects.size()) {
            return null;
        }
        final GenericObject genericObject = this.mGenericObjects.get(position)
                .clone();
        this.mGenericObjects.remove(position);
        notifyDataSetChanged();
        return genericObject;
    }

    @Override
    public int getCount() {
        return this.mGenericObjects.size();
    }

    public GenericFile getFile(final GenericObject genericObject) {
        final GenericFile genericFile = new GenericFile();
        final String localPath = genericObject.getValue(this.mGallerySpec
                .getImagePathColumn());
        genericFile.setLocalPath(localPath);
        genericFile.setFileNameByURl(localPath);
        return genericFile;
    }

    public ArrayList<GenericObject> getGenericObjects() {
        return this.mGenericObjects;
    }

    @Override
    public Fragment getItem(final int arg0) {
        final GenericObject genericObject = this.mGenericObjects.get(arg0);
        final String imagePath = genericObject.getValue(this.mGallerySpec
                .getImagePathColumn());
        return PhotoFragment.newInstance(imagePath, this.mGallerySpec,
                this.mImageFetcher);
    }

    @Override
    public int getItemPosition(final Object object) {
        return PagerAdapter.POSITION_NONE;
    }

    public void setGenericObjects(final ArrayList<GenericObject> genericObjects) {
        this.mGenericObjects = genericObjects;
    }
}
